/*
 * @Author: zhangfeng
 * @Date: 2023-12-28 15:40:49
 * @LastEditTime: 2023-12-28 16:21:17
 * @LastEditors: zhangfeng
 * @Description:
 * @FilePath: \nodeapi\src\controller\role-user.controller.js
 * © 2023 张峰，All rights reserved.
 */
const { createServer } = require("../server/role-user.server");

const { roleUserCreateFormatterError } = require("../constants/err.type");

class RoleUserController {
  async create(ctx, next) {
    const { userId, roleId } = ctx.request.body;
    console.log(userId);
    console.log(roleId);
    const paramsArray = [];
    if (Array.isArray(userId) && Array.isArray(roleId)) {
      userId.map((user) => {
        roleId.map((role) => {
          paramsArray.push({
            userId: user,
            roleId: role,
          });
        });
      });
    } else if (Array.isArray(userId) && !Array.isArray(roleId)) {
      userId.map((user) => {
        paramsArray.push({
          userId: user,
          roleId: roleId,
        });
      });
    } else if (!Array.isArray(userId) && Array.isArray(roleId)) {
      roleId.map((role) => {
        paramsArray.push({
          userId: userId,
          roleId: role,
        });
      });
    } else {
      paramsArray.push({
        userId: userId,
        roleId: roleId,
      });
    }
    console.log(paramsArray);
    try {
      const result = await createServer(paramsArray);
      console.log(result);
      if (result) {
        ctx.body = {
          code: 0,
          message: "角色-用户绑定成功",
          result: "",
        };
      } else {
        ctx.body = roleUserCreateFormatterError;
      }
    } catch (error) {
      console.error(err);
      ctx.body = roleUserCreateFormatterError;
    }
  }
}

module.exports = new RoleUserController();
